<?php

class Users extends CI_Model {

    const Statustrue = True;
    const Statusflase = False;

    function __construct() {
        $this->load->helper('fuc');
        $this->load->library('topclass');
        $this->load->library('session_db');
        $this->config->load('topapiconfig');
        parent::__construct();
    }

    function get_userinfo_array() {// 根据昵称获取shoplogo 
        $top_session = $this->session_db->get_Sessiondb("top_session");
        $usernick = $this->session_db->get_Sessiondb("usernick");
        $res = $this->topclass->api_getUserGetRequest("UserSellerGetRequest", $usernick, $top_session);
        objectToArray(&$res);
        return $res;
    }

    function get_shopinfo_array() {//top授权处理流程
        //   $top_session = $this->session_db->get_Sessiondb("top_session");
        $usernick = $this->session_db->get_Sessiondb("usernick");
        //print_r($usernick);exit;
        $res = $this->topclass->api_getShopGetRequest("ShopGetRequest", $usernick);
        objectToArray(&$res);
        return $res;
    }

    function get_appgroup_array() {//获取用户订购app对应系统中那个权限
    }

    function user_process() {//这里面为什么会有注释呢，是因为userid的确定那个问题暂时没有删除
        $userinfo_array = $this->get_userinfo_array();

        $shopinfo_array = $this->get_shopinfo_array();

        if (isset($userinfo_array["user"]) && isset($shopinfo_array["shop"])) {
            //   $sql = "select * from  user where  shop_id=\"{$shopinfo_array["shop"]["sid"]}\"";
            $sql = "select * from  user where  user_id=\"{$userinfo_array["user"]["user_id"]}\"";

            $query = $this->db->query($sql);
            $res = $query->result_array();
            $first_login = time();
           // print_r($res);
            if (empty($res)) {
                $sql = "insert into user(nick,shop_id,user_id,user_type,
shop_logo,
level,
first_login,
user_group)values(
\"{$userinfo_array["user"]["nick"]}\",
    \"{$shopinfo_array["shop"]["sid"]}\",\"{$userinfo_array["user"]["user_id"]}\",
        \"{$userinfo_array["user"]["type"]}\",
            \"{$shopinfo_array["shop"]["pic_path"]}\",
                \"{$userinfo_array["user"]["seller_credit"]["level"]}\",
                    \"{$first_login}\",0)";
                $query = $this->db->query($sql);
//                $sql = "select id  from  user order by id desc limit 1";
//                $query = $this->db->query($sql);
//                $res = $query->result_array();
                $this->session_db->write_Sessiondb("userid_my", $userinfo_array["user"]["user_id"]); //把获得的user_id保存入session
                //
              //  $updatesql = "update user set uid=\"{$res[0]["id"]}\" where id= \"{$res[0]["id"]}\"";
                //  $query = $this->db->query($updatesql);
                //print_r( $res);
            } else {//更新
                $updatesql = "update user set user_type=\"{$userinfo_array["user"]["type"]}\",shop_logo=\"{$shopinfo_array["shop"]["pic_path"]}\",
 level=\"{$userinfo_array["user"]["seller_credit"]["level"]}\",first_login=\"{$first_login}\",
               user_group=0  where  shop_id=\"{$shopinfo_array["shop"]["sid"]}\"   ";
                $this->db->query($updatesql);
                $sql = "select * from  user where  shop_id=\"{$userinfo_array["user"]["user_id"]}\"";
                $query = $this->db->query($sql);
                $res = $query->result_array();
                $this->session_db->write_Sessiondb("userid_my", $userinfo_array["user"]["user_id"]);
                //还要在sessiondb中保存
                //自拟uid
            }
        }
    }

}

?>
